package org.hhchat.leetcode.dp;

/**
 * Created this one by HMH on 2017/6/11.
 * Arithmetic Slices
 */
public class code413 {
    public static class  Solution {
        public int numberOfArithmeticSlices(int[] A) {
            if(A==null||A.length<3)
                return 0;
            int dis[] = new int[A.length-1];
            int i;
            //找到所有的间隔
            for(i=0;i<A.length-1;i++) {
                dis[i]=A[i+1]-A[i];
            }
            //找到所有的slices
            int result = 0;
            int len=1;
            for(i=0;i<dis.length;i++) {
                if (i > 0 && dis[i] == dis[i - 1]) {
                    len++;
                }else if(i==0){
                    len=2;
                }else{
                    if(len>=3){
                        //个数也是等差数列
                        int temp = (len-3)+1;
                        result=result+(temp+1)*temp/2;
                    }
                    len=2;
                }
            }
            if(len>=3){
                int temp = (len-3)+1;
                result=result+(temp+1)*temp/2;
            }
            return result;
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.numberOfArithmeticSlices(new int[]{1,2,3,4}));
    }
}

/*
 * leetcode上看到的9行代码
 */

//    public int numberOfArithmeticSlices(int[] A) {
//        int curr = 0, sum = 0;
//        for (int i=2; i<A.length; i++)
//            if (A[i]-A[i-1] == A[i-1]-A[i-2]) {
//                curr += 1;
//                sum += curr;
//            } else {
//                curr = 0;
//            }
//        return sum;
//    }